package com.leetcode.LC;

import java.util.*;

public class LC49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length == 0) {
            return new ArrayList<List<String>>();
        }
        HashMap<String, List<String>> stringArrayListHashMap = new HashMap<>();
        ArrayList<List<String>> lists = new ArrayList<>();
        int[] ints = new int[26];
        for (String str : strs) {
            Arrays.fill(ints, 0);

            char[] chars = str.toCharArray();
            for (char aChar : chars) {
                ints[aChar - 'a']++;
            }
            String key = Arrays.toString(ints);
            //System.out.println(key);

            if (!stringArrayListHashMap.containsKey(key)) {
                stringArrayListHashMap.put(key, new ArrayList<>());
            }
            stringArrayListHashMap.get(key).add(str);
        }

        for (List<String> value : stringArrayListHashMap.values()) {
            lists.add(value);
        }
        return lists;
    }

    public static void main(String[] args) {
        String[] strings = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
        for (List<String> groupAnagram : new LC49().groupAnagrams(strings)) {
            System.out.println(groupAnagram);
        }
    }
}
